<?php
namespace app\system\controller;

use app\common\model\AuthGroupModel;
use app\common\model\UserModel;
use think\helper\Str;
use think\Request;

class UserController extends BaseController
{

    private $utype;
    private $userModel;
    private $authGroupModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->userModel      = new UserModel();
        $this->authGroupModel = new AuthGroupModel();
        $this->utype          = $this->request->param('utype', $this->getUtype());
    }

    /**
     * 会员首页
     * @return mixed
     */
    public function index()
    {
        $map          = [];
        $query        = [];
        $map['utype'] = $this->utype;
        $searchKey    = $this->request->param('searchKey');
        $searchText   = $this->request->param('searchText');
        if (!empty($searchKey) && !empty($searchText)) {
            $map[$searchKey]     = ['like', '%' . $searchText . '%'];
            $query['searchKey']  = $searchKey;
            $query['searchText'] = $searchText;
        }
        $model = $this->userModel->getListModel($this->utype);
        $list  = $model->where($map)->order('create_time desc')->paginate(12, false, ['query' => $query]);
        $this->assign('utype', $this->utype);
        $this->assign('list', $list);
        return $this->fetch($this->utype . '_index');
    }

    /**
     * 添加用户
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $data['site_id'] = $this->getSiteId();
            $result = $this->validate($data, 'User' . Str::studly($data['utype']) . 'Validate.add');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->userModel->addData($data)) {
                $uid = $this->userModel->uid;
                if($data['utype'] == "site"){
                    $this->userModel->where('uid',$uid)->setField('site_id',$uid); //设置创始人
                }
                $this->success('添加成功！', url('index', ['utype' => $data['utype']]));
            } else {
                $this->error($this->userModel->getError());
            }
        }
        $groupList = $this->authGroupModel->where('utype', $this->utype)->order('group_sort asc')->select();
        $this->assign('groupList', $groupList);
        $this->assign('utype',$this->getUtype());
        $this->assign('tradeList',cache('trade'));
        return $this->fetch($this->utype . '_add');
    }

    /**
     * 修改数据
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'User' . Str::studly($data['utype']) . 'Validate.edit');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->userModel->editData($data)) {
                $this->success('修改成功！', url('index', ['utype' => $data['utype']]));
            } else {
                $this->error($this->userModel->getError());
            }
        }
        $uid       = $this->request->param('uid');
        $info      = $this->userModel->getInfo($this->utype, $uid);
        $groupList = $this->authGroupModel->where('utype', $this->utype)->order('group_sort asc')->select();
        $this->assign('groupList', $groupList);
        $this->assign('info', $info);
        $this->assign('utype',$this->getUtype());
        $this->assign('tradeList',cache('trade'));
        return $this->fetch($this->utype . '_edit');
    }

    /**
     * 修改密码
     */
    public function editpass()
    {
        if ($this->request->isAjax()) {
            $oldPassword     = $this->request->param('old_password');
            $newPassword     = $this->request->param('new_password');
            $replayPassword  = $this->request->param('replay_password');
            $map['uid']      = $this->getUid();
            $map['password'] = md5($oldPassword);
            if ($this->userModel->where($map)->find()) {
                if ($newPassword == $replayPassword) {
                    $data['uid']      = $this->getUid();
                    $data['password'] = $newPassword;
                    $this->userModel->editData($data);
                    cookie('systemDes', null);
                    $this->success('密码修改成功,请重新登陆！', url('editpass'));
                } else {
                    $this->error('原密码和确认密码不一致！');
                }
            } else {
                $this->error('原密码输入错误！');
            }

        }
        $info = $this->userModel->get($this->getUid());
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除数据
     */
    public function del()
    {
        $uid = $this->request->param('uid', 0);
        if ($this->userModel->delData($uid)) {
            $this->success('删除成功！');
        } else {
            $this->error($this->userModel->getError());
        }
    }

    /**
     * 店铺信息
     * @return mixed
     */
    public function shopedit(){
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'UserSiteValidate.site');
            if (true !== $result) {
                $this->error($result);
            }
            $data['uid'] = $this->getSiteId();
            if ($this->userModel->editData($data)) {
                $this->success('修改成功！', url('shopedit'));
            } else {
                $this->error($this->userModel->getError());
            }
        }
        $info      = $this->userModel->getInfo('site', $this->getSiteId());
        $this->assign('info', $info);
        return $this->fetch();
    }

}
